<template>
  <div class="app-container">
    <!--操作栏-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item v-if="isPrivilege('jzx-resource-save')">
        <router-link :to="{path:'/resource/add'}">
          <el-button v-waves clearable class="filter-item" type="primary" icon="el-icon-plus">添加</el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <!--表格主体-->
    <div>
      <tree-table v-loading="listLoading" :data="data" :columns="columns" border highlight-current-row>
        <el-table-column label="资源代码" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.resourceCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="资源类型" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.resourceType | resourceTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="链接地址" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.uri }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="isPrivilege('jzx-resource-del') || isPrivilege('jzx-resource-update')" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <router-link :to="`/resource/edit/${scope.row.id}`">
              <el-button v-if="isPrivilege('jzx-resource-update')" size="mini" type="primary">编辑</el-button>
            </router-link>
            <el-button v-if="isPrivilege('jzx-resource-del')" style="margin-left: 10px" size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </tree-table>
    </div>
  </div>
</template>

<script>

import treeTable from '@/components/TreeTable'
import { resourceTree, delResource } from '@/api/system/resource'
import waves from '@/directive/waves' // waves directive
import { isPrivilege } from '@/utils/constUtils'

export default {
  name: 'ResourceTree',
  components: { treeTable },
  directives: { waves },
  filters: {
    resourceTypeFilter(status) {
      const typeMap = {
        0: '模块',
        1: '功能'
      }
      return typeMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        0: '隐藏',
        1: '显示'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      columns: [
        {
          text: '资源名称',
          value: 'resourceName',
          width: 260
        }],
      data: {},
      listLoading: true,
      listLoading1: true
    }
  },
  created() {
    this.getTree()
  },
  methods: {
    isPrivilege(resourceCode) {
      return isPrivilege(resourceCode)
    },
    async getTree() {
      this.listLoading = true
      await resourceTree({}).then(response => {
        this.data = response.data
        this.listLoading = false
      })
    },
    handleDelete(row) {
      const resource = Object.assign({}, row) // copy obj
      this.$confirm('此操作将永久删除该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delete resource['parent'] // 删除parent属性，否则JSON.stringify()报错
        this.deleteData(resource.id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deleteData(id) {
      this.listLoading = true
      delResource({ id: id }).then(response => {
        const returnCode = response && response.returnCode
        if (this.Const.OK === returnCode) {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getTree()
        } else {
          this.$message({
            type: 'error',
            message: response && response.msg || '更新失败'
          })
          this.listLoading = false
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
